package io.adminboot.web;

import io.adminboot.domain.param.JqGridParam;
import io.adminboot.domain.vo.PageVO;
import io.adminboot.entity.Article;
import io.adminboot.service.IArticleService;
import io.adminboot.system.annotation.AddSysLog;
import io.adminboot.system.validator.ValidatorUtils;
import io.adminboot.system.validator.group.AddGroup;
import io.adminboot.system.validator.group.UpdateGroup;
import io.adminboot.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article/article")
public class ArticleController extends AbstractController {
	@Autowired
	private IArticleService articleService;

	/**
	 * 文章列表
	 */
	@GetMapping("/list")
//	@RequiresPermissions("article:article:list")
	public R list(JqGridParam param) {
		String title = null;
		Map<String, String> keywords = param.getKeywords();
		if (keywords != null && keywords.get("title") != null) {
			title = keywords.get("title");
		}
		PageVO vo = articleService.queryPage(title, param.getPageNum(), param.getPageSize());
		return R.ok().put("page", vo);
	}
	
	/**
	 * 详情
	 */
	@GetMapping("/info/{id}")
//	@RequiresPermissions("article:article:info")
	public R info(@PathVariable("id") Integer id){
		Article article = articleService.selectById(id);
		return R.ok().put("article", article);
	}
	
	/**
	 * 保存
	 */
	@AddSysLog("保存文章")
	@PostMapping("/save")
//	@RequiresPermissions("article:article:save")
	public R save(@RequestBody Article article){
		ValidatorUtils.validateEntity(article, AddGroup.class);
		articleService.save(article);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@AddSysLog("修改文章")
	@PostMapping("/update")
//	@RequiresPermissions("article:article:update")
	public R update(@RequestBody Article article){
		ValidatorUtils.validateEntity(article, UpdateGroup.class);
		articleService.update(article);
		return R.ok();
	}
	
	/**
	 * 删除文章
	 */
	@AddSysLog("删除文章")
	@PostMapping("/delete")
//	@RequiresPermissions("article:article:delete")
	public R delete(@RequestBody Long[] ids){
		articleService.deleteBatch(ids);
		return R.ok();
	}
}
